<?php
if (@$_POST['open'] != 'please') {
    exit;
}
include_once('../../php/include_all.php');
?>
<div class="widget-header widget-header-flat ">
    <h5><i class="icon-calendar"></i>Sub Bidang Bappeda</h5>
</div>
<div class="position-relative" id="page-content">
    <a href="#modalwin" data-toggle="modal" class="btn btn-small btn-primary2 no-radius btn-tambah"><i class="icon-plus"></i>&nbsp;Tambah Data</a>
    &nbsp;<span id="load" class="spinner"></span>
</div>

<div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

    <header class="modal-header">
        <a href="#" class="close" data-dismiss="modal">x</a>
        <h3>Data Sub Bidang</h3>
    </header>

    <div class="modal-body">
        <form id="formSUBBID">
            <input type="hidden" name="act" value="simpan_subbid">
            <input type="hidden" id="id_subbidang" name="id_subbidang" value="0"> 
            <table class="table-form">
                <tbody>
                    <tr>
                        <td valign="middle">Nama Bidang</td>
                        <td valign="middle">:</td>
                        <td>
                            <select name="id_bdgbapeda" id="id_bdgbapeda"><option value="0" id='id_klasi'>-pilih bidang-</option>
                                <?php
                                $level = get_datas("select * from mus_bidang_bappeda order by id_bdgbapeda");
                                foreach ($level as $level) {
                                    ?>
                                    <option value="<?php echo $level['id_bdgbapeda']; ?>"><?php echo $level['nama_bdgbapeda']; ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td valign="middle">Kode Sub Bidang</td>
                        <td valign="middle">:</td>
                        <td>
                            <input type="text" name="kode_subbidang" id="kode_subbidang" class="xxlarge" readonly />
                        </td>
                    </tr>
                    <tr>
                        <td valign="middle">Nama Sub Bidang</td>
                        <td valign="middle">:</td>
                        <td>
                            <input type="text" name="nama_subbidang" id="nama_subbidang" class="xxlarge">
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td>
                            <a class="btn btn-primary2 btn-small bt-simpan"><div id="loader"></div><i class="icon-save"></i>&nbsp;Simpan</a><span id="result"></span>
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </div>
</div>
<div class="box-content">
    <table class="table table-bordered geo-table table-hover" width="80%">
        <thead> 
            <tr>
                <th class="center" width="15%">Kode Bidang/Subbidang</th>
                <th class="center" width="50%">Nama</th>
                <th class="center" width="25%">Aksi</th>
            </tr>
        </thead>
        <tbody id="tampilSUBBID">
            <?php
         //   $no = 1;
            $bidang = get_datas("select * from mus_bidang_bappeda order by id_bdgbapeda");
			$idbdf = " ";
			foreach ($bidang as $rw1) {
				$id1 = $rw1['id_bdgbapeda'];
				if($idbdf != $id1){
					$idbdf = $id1;
					?><tr>
						<td><?php echo $id1 ?></td>
						<td><b><?php echo $rw1['nama_bdgbapeda'] ?></b></td>
						<td>&nbsp;</td>
					</tr>
					<?php
					$subbid = get_datas("select * from mus_subbidangbappeda where id_bdgbapeda = " . $rw1['id_bdgbapeda'] . "");
					foreach ($subbid as $rw2){
						?><tr>
							<td class="center"><?php echo $rw2['kode_subbidang'] ?></td>
							<td><?php echo $rw2['nama_subbidang'] ?></td>
							<td class="center">
								<a href="#modalwin" data-toggle="modal"  class="btn btn-info btn-small bt-edit"  name="<?php echo $rw2['id_subbidangbappeda'] ?>"><i class="icon-edit icon-white"></i> ubah</a>
								<a class='btn btn-danger btn-small bt-hapus' name="<?php echo $rw2['id_subbidangbappeda'] ?>"><i class="icon-trash icon-white"></i> hapus</a>   
							</td>
						</tr>
						<?php
						$no++;
					}
				//		$no++;
					//$no++;
				}
            }
            ?>
        </tbody>
    </table>    
</div>
<script type="text/javascript">
    var url = 'admin/master_subbappeda/subbid_save.php';
    var rslt = $('#result');
    
    $('#id_bdgbapeda').change(function(){
        source ="admin/master_subbappeda/id.php?idb="+this.value;
        var coba=$.get(source);
        coba.done(function(data){          
            document.getElementById('kode_subbidang').value=data;	
        })
    });
    
    $('.bt-simpan').click(function(){
        var btn = $(this);
        var load = $('#loader');
        var rslt = $('#result');
		   
        load.addClass('icon-spinner icon-spin icon-2x white');
   //     btn.removeClass('btn-primary2').addClass('btn-info');
        
        var form = $('#formSUBBID');
        var data = form.serializeArray();
        var post = $.post(url, data);
        post.done(function(res){
            var result = res.split('__');
            
            if(result.length==3){
                if(result[0]=='success'){
                    rslt.html('<font color="green">Data Tersimpan...</font>');
                 //   btn.removeClass('btn-info').addClass('btn-primary2');
                    setTimeout(function(){
                        rslt.html('');
                    },1500);
                
                    var tbody = $('#tampilSUBBID');
                    tbody.html(result[2]);
                    init();
					$("#id_subbidang").val('0');
                    document.getElementById("id_bdgbapeda").selected = true;
                    $("#nama_subbidang").val("");
                    $("#kode_subbidang").val("");

                   $('#modalwin').modal('hide');
                }else{
                    rslt.html('<font color="red">'+res+'</font>');
                }
            }else{
                rslt.html('<font color="red">'+res+'</font>')
            }
            load.removeClass();
            btn.removeClass('.btn-info').addClass('.btn-primary2');
        });
    });
    
    $('.btn-tambah').click(function(){ 
		$("#id_subbidang").val('0');
        document.getElementById("id_klasi").selected = true;
        $("#nama_subbidang").val('');
        $("#kode_subbidang").val('');            
    });
    
    function init(){
    
        $('.bt-edit').click(function(){
            var id_subbidang = this.name;
            var form = $('#formSUBBID');
            var post = $.post(url,{act:'ubah_subbid',id_subbidang:id_subbidang});
            post.done(function(res){
                var value = res.split('__');  
                $('#id_subbidang').val(value[0]);
                form.find('select[name="id_bdgbapeda"]').val(value[1]); 
                form.find('input[name="kode_subbidang"]').val(value[2]);
                form.find('input[name="nama_subbidang"]').val(value[3]);
            });
        });
        
        $('.bt-hapus').click(function(){
            var btn = $(this);
            var pid = this.name;
            var load = $(this);
            load.html('<div class="icon-spinner icon-spin icon-2x white"></div>');
            bootbox.confirm("Anda akan menghapus subbidang ini ?", function(hsl){
                if(hsl==true){
                    if(pid!=0){
                        var post = $.post(url,{act:"hapus_subbid",id_subbidang:pid});
                        post.done(function(res){
                            var hasil = res.split('__');
                            //alert(hasil.length);
                            if(hasil.length == 2){
                                if(hasil[0] == 'success'){
                                    rslt.html('<font color="green">Data Telah Dihapus...</font>');
                                    setTimeout(function(){
                                        rslt.html('');
                                    }, 1500);
                                    var tbody = $('#tampilSUBBID');
                                    tbody.html(hasil[1]);
                                    init();
                                    //btn.parent().parent().remove();
                                }
                            }
                        });
                        load.html('<i class="icon-trash icon-white"></i> hapus</a>   ');
                    }
                }else{
                    load.html('<i class="icon-trash icon-white"></i> hapus</a>   ');
                }
            });
        });
          
    }
/*
$('.close').click(function(){
	$('#id_subbidang').val(0);
	document.getElementById('formSUBBID').reset();
});*/
      init();
</script>